<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <!-- <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button> -->
        <el-button icon="Refresh" @click="resetQuery" style="background-color: rgb(100,100,100);color: white;"></el-button>
      </el-form-item>
      <el-form-item label="套餐名称" prop="comboId" style="margin-left: 10%;">
        <el-input v-model="queryParams.comboName" placeholder="请输入套餐名称" clearable @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item label="司机名称" prop="userName">
        <el-input
            v-model="queryParams.userName"
            placeholder="请输入司机名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item
          label="购买方式"
          prop="type"
        >
          <el-select
            v-model="queryParams.purchaseWay"
            placeholder="请选择购买方式"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          >
            <el-option
              v-for="(item, index) in zifu"
              :key="index"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

      <el-form-item label="购买时间" style="width: 308px;">
        <el-date-picker
            v-model="dateRange"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery" style="background-color: rgb(240,240,240);
        color: black;
        font-weight: bold;
        border: 1px solid rgb(180,180,180);
        margin-left: -16px;
         "></el-button>
        <!-- <el-button icon="Refresh" @click="resetQuery" ></el-button> -->
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:shopjilu:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:shopjilu:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:shopjilu:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:shopjilu:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="shopjiluList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="purchaseId" />
      <el-table-column label="套餐名称" align="center" prop="comboName" />
      <el-table-column label="司机ID" align="center" prop="driverId" />
      <el-table-column label="司机名称" align="center" prop="userName" />

      <el-table-column label="购买金额" align="center" prop="purchaseAmount" />
      <el-table-column label="购买方式" align="center" prop="purchaseWay">
        <template #default="scope">
          {{ scope.row.purchaseWay == 0 ? "微信支付" : scope.row.purchaseWay == 1 ? "支付宝支付" : ''}}
        </template>
      </el-table-column>
      <el-table-column label="到期时间" align="center" prop="endTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="购买时间" align="center" prop="purchaseTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.purchaseTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:shopjilu:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:shopjilu:remove']">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改购买记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="shopjiluRef" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Shopjilu">
import { listShopjilu, getShopjilu, delShopjilu, addShopjilu, updateShopjilu } from "@/api/system/shopjilu";
import { ref } from "vue";

const { proxy } = getCurrentInstance();
const dateRange = ref([]);
const shopjiluList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const zifu = ref([
    {
      label: "微信方式",
      value:0
    },
    {
      label: "支付宝方式",
      value:1
    }
]);
const searchtime = ref(null);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    comboId: null,
    purchaseWay: null,
    purchaseTime: null,
  },
  rules: {
    comboId: [
      { required: true, message: "套餐ID不能为空", trigger: "blur" }
    ],
    driverId: [
      { required: true, message: "司机ID不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询购买记录列表 */
function getList() {
  loading.value = true;

  listShopjilu(proxy.addDateRange(queryParams.value,dateRange.value)).then(response => {
    shopjiluList.value = response.rows;
    total.value = response.total;
    loading.value = false;
    searchtime.value = null
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    purchaseId: null,
    comboId: null,
    driverId: null,
    purchaseAmount: null,
    purchaseWay: null,
    purchaseTime: null,
    endTime: null
  };
  proxy.resetForm("shopjiluRef");
}

/** 搜索按钮操作 */
function handleQuery() {

  if(searchtime.value){
    queryParams.value.startSift = searchtime.value[0];
    queryParams.value.endSift = searchtime.value[1];
  }else{
    queryParams.value.startSift = "";
    queryParams.value.endSift = "";
  }
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  //清空套餐名称输入框里的内容
  queryParams.value.comboName = "";
  // 清空下拉框的值
  queryParams.value.purchaseWay = null;
  proxy.resetForm("queryRef");
  dateRange.value = [];
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.purchaseId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加购买记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _purchaseId = row.purchaseId || ids.value
  getShopjilu(_purchaseId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改购买记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["shopjiluRef"].validate(valid => {
    if (valid) {
      if (form.value.purchaseId != null) {
        updateShopjilu(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addShopjilu(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _purchaseIds = row.purchaseId || ids.value;
  proxy.$modal.confirm('是否确认删除购买记录编号为"' + _purchaseIds + '"的数据项？').then(function() {
    return delShopjilu(_purchaseIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/shopjilu/export', {
    ...queryParams.value
  }, `shopjilu_${new Date().getTime()}.xlsx`)
}

getList();
</script>
